package com.frank.loadinglibrary.common.ring;

import com.frank.loadinglibrary.common.BaseLoadingComponent;
import ohos.agp.components.Attr;
import ohos.agp.components.AttrSet;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.Optional;

/**
 * @author SUQI
 * @date 2021/4/29
 * @description
 **/
public abstract class BaseRingLoadingComponent<T extends BaseRingLoadingComponent> extends BaseLoadingComponent {

    public float strokeWidth = 10;
    public boolean isFill = false;
    public float startAngle = 0;
    public float sweepAngle = 270;
    public Color startColor = Color.WHITE;
    public Color endColor = Color.TRANSPARENT;
    public Color[] colors = new Color[2];

    public BaseRingLoadingComponent(Context context) {
        this(context, null);
    }

    public BaseRingLoadingComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }

    public BaseRingLoadingComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initAttr(attrSet);
    }

    public BaseRingLoadingComponent(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
    }

    private void initAttr(AttrSet attrSet) {
        if (null != attrSet) {
            Optional<Attr> attrStrokeWidth = attrSet.getAttr("strokeWidth");
            attrStrokeWidth.ifPresent(attr -> {
                strokeWidth = attr.getFloatValue();
            });
            Optional<Attr> attrIsFill = attrSet.getAttr("isFill");
            attrIsFill.ifPresent(attr -> {
                isFill = attr.getBoolValue();
            });
            Optional<Attr> attrStartAngle = attrSet.getAttr("startAngle");
            attrStartAngle.ifPresent(attr -> {
                startAngle = attr.getIntegerValue();
            });
            Optional<Attr> attrSweepAngle = attrSet.getAttr("sweepAngle");
            attrSweepAngle.ifPresent(attr -> {
                sweepAngle = attr.getIntegerValue();
            });
            Optional<Attr> attrStartColor = attrSet.getAttr("startColor");
            attrStartColor.ifPresent(attr -> {
                startColor = attr.getColorValue();
            });
            Optional<Attr> attrEndColor = attrSet.getAttr("endColor");
            attrEndColor.ifPresent(attr -> {
                endColor = attr.getColorValue();
            });
        }
        colors = new Color[]{startColor, endColor};
    }

    public T setStrokeWidth(float strokeWidth) {
        this.strokeWidth = strokeWidth;
        return (T) this;
    }

    public T setFill(boolean fill) {
        isFill = fill;
        return (T) this;
    }

    public T setStartAngle(float startAngle) {
        this.startAngle = startAngle;
        return (T) this;
    }

    public T setSweepAngle(float sweepAngle) {
        this.sweepAngle = sweepAngle;
        return (T) this;
    }

    public T setStartColor(Color startColor) {
        this.startColor = startColor;
        return (T) this;
    }

    public T setEndColor(Color endColor) {
        this.endColor = endColor;
        return (T) this;
    }

    public T setColors(Color[] colors) {
        this.colors = colors;
        return (T) this;
    }
}
